﻿using DMC.Core.Models.Dto;
using DMC.Core.Models.Queries;
using DMC.Core.Providers;
using MiniFox.Components;
using MiniFox.Data;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;
using System.Data;

namespace DataService.Data
{
    [AutoResolve]
    public class ConnectionProvider : EntityDataProvider<PlatformDbContext>, IConnectionProvider
    {
        public ResultObject<object[]> GetDbProviders()
        {
            var providers = Database.Configuration.ProviderSettings;
            var result = new ResultObject<object[]>(providers.Count);
            result.Result = providers.Select(p => new { p.Name, p.DisplayName }).ToArray();
            return result;
        }

        public ResultObject<IEnumerable<ConnectionDto>> Query(QueryObject<ConnectionQuery> request)
        {
            var result = this.QueryExecute<DsConnection>(request);
            return result.GetResultObject(c => new ConnectionDto
            {
                Name = c.Name,
                Provider = c.ProviderName,
                ConnectionString = c.ConnectionString
            });
        }

        public void Test(string provider, string connectionString)
        {
            try
            {
                Database database = Database.CreateDatabase(connectionString, provider);
                database.RaiseException = true;
                database.TestConnection();
            }
            catch (Exception e)
            {
                Exception exception = e;
                while (exception.InnerException != null)
                {
                    exception = exception.InnerException;
                }
                throw new MiniFox.Exceptions.SystemException(exception.Message);
            }
        }

        public bool Create(ConnectionDto connection)
        {
            var conn = new DsConnection();
            conn.Name = connection.Name;
            conn.ProviderName = connection.Provider;
            conn.ConnectionString = connection.ConnectionString;
            return this.Insert(conn) > 0;
        }

        public bool Delete(ConnectionDto connection)
        {
            var conn = this.CurrentDbContext.DsConnection.SingleOrDefault(c => c.Name == connection.Name);
            return this.Delete(conn) > 0;
        }

        public bool Update(ConnectionDto connection)
        {
            var conn = this.CurrentDbContext.DsConnection.SingleOrDefault(c => c.Name == connection.Name);

            conn.ProviderName = connection.Provider;
            conn.ConnectionString = connection.ConnectionString;
            return this.Update(conn) > 0;
        }

        #region connection

        public void Sync()
        {
            //foreach (ConnectionStringSettings setting in ConfigurationManager.ConnectionStrings)
            //{
            //    var conn = this.CurrentDbContext.DsConnection.SingleOrDefault(c => c.Name == setting.Name);
            //    try
            //    {

            //        if (conn == null)
            //        {
            //            conn = this.CurrentDbContext.DsConnection.Create();
            //            conn.Name = setting.Name;
            //            conn.ConnectionString = setting.ConnectionString;
            //            conn.ProviderName = setting.ProviderName;
            //            this.Create(conn);
            //        }
            //        else
            //        {
            //            conn.ConnectionString = setting.ConnectionString;
            //            conn.ProviderName = setting.ProviderName;
            //            this.Update(conn);
            //        }
            //    }
            //    catch
            //    {

            //    }
            //}
        }

        #endregion

    }
}
